﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace IFRS.Web
{
    public class Utility
    {
        /// <summary>
        /// Register JavaScript "alert"
        /// </summary>
        /// <param name="pg">Always send Page as parameter</param>
        /// <param name="strMessage">Message text that will display alert</param>
        public static void AlertMessage(Page pg, string strMessage)
        {
            string strScript = "alert('" + strMessage + "');";
            ScriptManager.RegisterStartupScript(pg, pg.GetType(), (new Guid()).ToString(), strScript, true);
        }

        /// <summary>
        /// Convert DateTime to full date and time represent in Thai culture
        /// </summary>
        /// <param name="InputDate">DateTime that will converted</param>
        /// <returns>Full date and time in Thai</returns>
        public static string LongDateTimeTH(DateTime InputDate)
        {
            string DateTimeTH = "";
            string Day = "";
            string Month = "";
            string Year = "";
            string Time = InputDate.Hour.ToString() + ":" + InputDate.Minute.ToString("00") + ":" + InputDate.Second.ToString("00");

            switch (InputDate.DayOfWeek.ToString())
            {
                case "Sunday": Day = "วันอาทิตย์"; break;
                case "Monday": Day = "วันจันทร์"; break;
                case "Tuesday": Day = "วันอังคาร"; break;
                case "Wednesday": Day = "วันพุธ"; break;
                case "Thursday": Day = "วันพฤหัสบดี"; break;
                case "Friday": Day = "วันศุกร์"; break;
                case "Saturday": Day = "วันเสาร์"; break;
                default: Day = InputDate.DayOfWeek.ToString(); break;
            }

            switch (InputDate.Month)
            {
                case 1: Month = "มกราคม"; break;
                case 2: Month = "กุมภาพันธ์"; break;
                case 3: Month = "มีนาคม"; break;
                case 4: Month = "เมษายน"; break;
                case 5: Month = "พฤษภาคม"; break;
                case 6: Month = "มิถุนายน"; break;
                case 7: Month = "กรกฎาคม"; break;
                case 8: Month = "สิงหาคม"; break;
                case 9: Month = "กันยายน"; break;
                case 10: Month = "ตุลาคม"; break;
                case 11: Month = "พฤศจิกายน"; break;
                case 12: Month = "ธันวาคม"; break;
                default: break;
            }

            if (InputDate.Year < 2500)
            {
                Year = (InputDate.Year + 543).ToString();
            }
            else
            {
                Year = InputDate.Year.ToString();
            }

            DateTimeTH = Day + " ที่ " + InputDate.Day.ToString() + " " + Month + " " + Year + " เวลา " + Time;

            return DateTimeTH;
        }

        public static void BindDropDownList(ref DropDownList ddl, string Query, string ValueField, string TextField, int SelectedIndex = 0)
        {
            if (Query == null || Query == "")
            {
                return;
            }

            DataTable dt;
            try
            {
                dt = Database.GetDataTable(Database.IFRSConnection(), CommandType.Text, Query);
                if (dt.Rows.Count > 0)
                {
                    ddl.DataSource = dt;
                    ddl.DataValueField = ValueField;
                    ddl.DataTextField = TextField;
                    ddl.SelectedIndex = SelectedIndex;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// เรียงลำดับรูปแบบวันที่ใหม่เป็น yyyy-MM-dd (ปี ค.ศ.)
        /// </summary>
        /// <param name="Date">วันที่ที่ต้องการจัดเรียงใหม่</param>
        /// <param name="Type">ประเภทของวันที่เดิม</param>
        /// <returns></returns>

        public static string ReorganizeDateString(string Date, string Type)
        {
            if (Date == "")
            {
                return Date;
            }

            int Day = 0, Month = 0, Year = 0;
            string[] SplitString = Date.Split('/');
            string OrganizedDate = "";

            switch (Type.ToUpper())
            {
                case "DATE":
                    Day = Convert.ToInt32(SplitString[0]);
                    Month = Convert.ToInt32(SplitString[1]);
                    Year = (Convert.ToInt16(SplitString[2]));

                    break;
                case "MONTH":
                    Month = Convert.ToInt32(SplitString[0]);
                    Year = Convert.ToInt32(SplitString[1]) - 543;
                    break;
                case "YEAR_BEFORE":
                    Month = Convert.ToInt32(SplitString[0]) * 3;
                    Year = Convert.ToInt32(SplitString[1]) - 1;
                    break;
                case "QUARTER":
                    Month = Convert.ToInt32(SplitString[0]) * 3;
                    Year = Convert.ToInt32(SplitString[1]);
                    break;
                case "BEGIN_QUARTER":
                    if (Convert.ToInt32(SplitString[0]) == 1)
                    {
                        Month = 1;
                        Year = Convert.ToInt32(SplitString[1]);
                    }
                    else if (Convert.ToInt32(SplitString[0]) == 2)
                    {
                        Month = 4;
                        Year = Convert.ToInt32(SplitString[1]);
                    }
                    else if (Convert.ToInt32(SplitString[0]) == 3)
                    {
                        Month = 7;
                        Year = Convert.ToInt32(SplitString[1]);
                    }
                    else if (Convert.ToInt32(SplitString[0]) == 4)
                    {
                        Month = 10;
                        Year = Convert.ToInt32(SplitString[1]);
                    }
                    break;

                default: break;
            }

            if (Day == 0)
            {
                switch (Month)
                {
                    case 1:
                        Day = 31;
                        break;
                    case 2:
                        if (DateTime.IsLeapYear(Year))
                            Day = 29;
                        else
                            Day = 28;
                        break;
                    case 3:
                        Day = 31;
                        break;
                    case 4:
                        Day = 30;
                        break;
                    case 5:
                        Day = 31;
                        break;
                    case 6:
                        Day = 30;
                        break;
                    case 7:
                        Day = 31;
                        break;
                    case 8:
                        Day = 31;
                        break;
                    case 9:
                        Day = 30;
                        break;
                    case 10:
                        Day = 31;
                        break;
                    case 11:
                        Day = 30;
                        break;
                    case 12:
                        Day = 31;
                        break;
                }
            }
            OrganizedDate = Year.ToString() + "-" + Month.ToString("00") + "-" + Day.ToString("00");

            return OrganizedDate;
        }



        public static string Reorganizeyear_en(string Date, string Type)
        {
            if (Date == "")
            {
                return Date;
            }

            int Day = 0, Month = 0, Year = 0;
            string[] SplitString = Date.Split('/');
            string OrganizedDate = "";

            switch (Type.ToUpper())
            {
                case "QUARTER":
                    Month = Convert.ToInt32(SplitString[0]) * 3;
                    Year = Convert.ToInt32(SplitString[1]);
                    break;
                default: break;
            }


            OrganizedDate = Year.ToString();

            return OrganizedDate;
        }
        public static string ThaiDate(string Date, string Type)
        {
            if (Date == "")
            {
                return Date;
            }

            int Day = 0, Month = 0, Year = 0;
            string Month2 = "";
            string[] SplitString = Date.Split('/');
            string OrganizedDate = "";

            switch (Type.ToUpper())
            {

                case "QUARTER":
                    Month = Convert.ToInt32(SplitString[0]) * 3;
                    Year = Convert.ToInt32(SplitString[1]);
                    break;
                default: break;
            }

            if (Day == 0)
            {
                switch (Month)
                {
                    case 1:
                        Month2 = "31 มกราคม";
                        break;
                    case 2:
                        if (DateTime.IsLeapYear(Year))
                            Month2 = "29 กุมภาพันธ์";
                        else
                            Month2 = "28 กุุมภาพันธ์";
                        break;
                    case 3:
                        Month2 = "31 มีนาคม";
                        break;
                    case 4:
                        Month2 = "30 เมษายน";
                        break;
                    case 5:
                        Month2 = "31 พฤษภาคม";
                        break;
                    case 6:
                        Month2 = "31 มิถุนายน";
                        break;
                    case 7:
                        Month2 = "31 กรกฎาคม";
                        break;
                    case 8:
                        Month2 = "31 สิงหาคม";
                        break;
                    case 9:
                        Month2 = "30 กันยายน";
                        break;
                    case 10:
                        Month2 = "31 ตุลาคม";
                        break;
                    case 11:
                        Month2 = "30 พฤศจิกายน";
                        break;
                    case 12:
                        Month2 = "31 ธันวาคม";
                        break;
                }
            }
            OrganizedDate = Month2.ToString() + Year.ToString();


            return OrganizedDate;
        }




        public static String toDate2(DateTime date)
        {
            string OrganizedDate = "";
            int Day = 0;


            if (Day == 0)
            {
                switch (date.Month)
                {
                    case 1:
                        Day = 31;
                        break;
                    case 2:
                        if (DateTime.IsLeapYear(date.Year))
                            Day = 29;
                        else
                            Day = 28;
                        break;
                    case 3:
                        Day = 31;
                        break;
                    case 4:
                        Day = 30;
                        break;
                    case 5:
                        Day = 31;
                        break;
                    case 6:
                        Day = 30;
                        break;
                    case 7:
                        Day = 31;
                        break;
                    case 8:
                        Day = 31;
                        break;
                    case 9:
                        Day = 30;
                        break;
                    case 10:
                        Day = 31;
                        break;
                    case 11:
                        Day = 30;
                        break;
                    case 12:
                        Day = 31;
                        break;
                }
            }

            // return date.Year + "" + date.Month.ToString("00") + "" + date.Day.ToString("00");
            OrganizedDate = ((Convert.ToInt16(date.Year)) + "-" + date.Month.ToString("00")) + "-" + Day.ToString();
            return OrganizedDate;
        }


        public static String toYear(DateTime date)
        {
            string OrganizedDate = "";
            // return date.Year + "" + date.Month.ToString("00") + "" + date.Day.ToString("00");
            OrganizedDate = Convert.ToInt16(date.Year).ToString();
            return OrganizedDate;
        }
    }
}